<template>
  <div class="login-container">
    <el-form ref="loginForm" v-if="!isLoginOveRegister" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">管理员登录</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="管理员账户" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input v-model="loginForm.password" :type="passwordType" name="password" auto-complete="on" show-password placeholder="管理员密码" @keyup.enter.native="handleLogin" />
      </el-form-item>

      <!-- <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="lock" />
        </span>
        <el-input v-model="loginForm.code" auto-complete="off" name="code"  placeholder="验证码" style="width: 60%" @keyup.enter.native="handleLogin" />
        <div class="login-code">
          <img :src="codeImg" @click="getCode">
        </div>
      </el-form-item> -->

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
    <!-- <el-form ref="registerForm" v-if="isLoginOveRegister" :model="registerForm" :rules="registerRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">商家注册</h3>
      </div>
      <el-form-item prop="username">
        <el-input v-model="registerForm.username" name="username" type="text" auto-complete="on" placeholder="注册用户名" />
      </el-form-item>

      <el-form-item prop="password">
        <el-input v-model="registerForm.password" name="password" auto-complete="on" placeholder="注册密码" />
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input v-model="registerForm.mobile" name="mobile" auto-complete="on" placeholder="注册手机号" />
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="registerForm.code" style="width:70%" name="code" auto-complete="on" placeholder="手机验证码" />
        <el-button :disabled='disabled' type="primary" @click="getPhoneCode">{{phoneText}}</el-button>
      </el-form-item>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleRegister">立即注册</el-button>

    </el-form> -->

    <!-- <div class="ruzhu" v-if='!isLoginOveRegister'>商家入驻：<span class="register_but" @click="register_but">立即注册</span></div>
        <div class="ruzhu" v-if='isLoginOveRegister'>已注册：<span class="register_but" @click="register_but">立即登录</span></div> -->

  </div>
</template>

<script>
import { getKaptcha, GMcaptcha, GMregister } from '@/api/login'
import { set } from 'js-cookie'

export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('管理员密码长度应大于6'))
      } else {
        callback()
      }
    }
    const validatePhone = (rule, value, callback) => {
      var ts = /^[1]([3-9])[0-9]{9}$/
      if (!ts.test(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    return {
      isLoginOveRegister: false,
      disabled: false,
      phoneText: '获取手机验证码',
      codeIndex: 60,
      registerForm: {
        username: '',
        password: '',
        mobile: '',
        code: ''
      },
      loginForm: {
        username: '',
        password: '',
        code: ''
      },
      codeImg: '',
      registerRules: {
        username: [
          { required: true, message: '注册账户不允许为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '注册密码不允许为空', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '注册手机号不允许为空', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '手机验证码不允许为空', trigger: 'blur' }
        ]
      },
      loginRules: {
        username: [
          { required: true, message: '管理员账户不允许为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '管理员密码不允许为空' },
          { validator: validatePassword, trigger: 'blur' }
        ]
      },
      passwordType: 'password',
      loading: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    register_but() {
      if (this.isLoginOveRegister) {
        (this.isLoginOveRegister = false),
        this.$refs.registerForm.clearValidate()
      } else {
        this.isLoginOveRegister = true
        this.$refs.loginForm.clearValidate()
      }
    },
    getCode() {
      getKaptcha().then(response => {
        this.codeImg = response.data.data
      })
    },
    getPhoneCode() {
      this.$refs.registerForm.validateField('mobile', res => {
        if (res == '') {
          if (!this.disabled) {
            GMcaptcha({
              mobile: this.registerForm.mobile
            }).then(res => {
              this.$notify({
                title: '获取验证码成功',
                message: res.data.errmsg,
                type: 'success'
              })
            })
          }
          this.disabled = true
          var time = setInterval(() => {
            this.codeIndex--
            this.phoneText = this.codeIndex + '秒后获取'
            if (this.codeIndex <= 0) {
              clearInterval(time)
              this.disabled = false
              this.phoneText = '获取手机验证码'
            }
          }, 1000)
        }
      })
    },
    handleRegister() {
      return
      this.$refs.registerForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          GMregister(this.registerForm)
            .then(res => {
              this.loading = false
              this.$notify({
                title: '注册成功',
                message: res.data.errmsg,
                type: 'success'
              })
              this.isLoginOveRegister = false
              this.loginForm.username = this.registerForm.username
              this.loginForm.password = this.registerForm.password
            })
            .catch(err => {
              this.loading = false
              this.$notify.error({
                title: '错误',
                message: err.data.errmsg
              })
            })
        }
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          this.$store
            .dispatch('LoginByUsername', this.loginForm)
            .then(() => {
              this.loading = false
              this.$router.push({ path: this.redirect || '/' })
            })
            .catch(response => {
              if (response.data.data) {
                this.codeImg = response.data.data
              }
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.ruzhu {
  text-align: center;
  color: #c0c4cc;
  font-size: 14px;
  .register_but {
    text-decoration: underline;
    cursor: pointer;
  }
  .register_but:hover {
    color: #fff;
  }
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      border: none;
      background: transparent;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .login-code {
    padding-top: 5px;
    float: right;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .copyright {
    font-size: 12px;
    color: #fff;
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translate(-50%, -50%);
    margin-bottom: 20px;
    letter-spacing: 0.6px;
    a {
      font-weight: bold;
      border-bottom: 1px solid #fff;
      font-family: "PingFangSC-Semibold", sans-serif;
    }
  }
}
</style>

